package net.zkbc.shiro.web.util;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import net.zkbc.shiro.web.filter.authc.JCaptchaValidateFilter;

public abstract class JCaptchaUtils {

	public static final String JCAPTCHA_ACCESS_DENIED = JCaptchaValidateFilter.class + ".ACCESS_DENIED";

	public static final String JCAPTCHA_PARAM_DEFAULT = "jcaptcha";

	public static final String JCAPTCHA_TYPE_LOGIN = "login";

	public static String getCaptchaID(String captchaType, HttpServletRequest request) {
		return request.getSession().getId() + "_" + captchaType;
	}

	public static String getLoginCaptchaID() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		return getCaptchaID(JCAPTCHA_TYPE_LOGIN, request);
	}

	public static boolean isAccessDenied(ServletRequest request) {
		return Boolean.TRUE.equals(request.getAttribute(JCaptchaUtils.JCAPTCHA_ACCESS_DENIED));
	}

	public static void setAccessDenied(ServletRequest request) {
		request.setAttribute(JCAPTCHA_ACCESS_DENIED, Boolean.TRUE);
	}

}
